package com.srit.system.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.srit.core.bean.Page;
import com.srit.system.bean.Event;
import com.srit.system.dao.EventDao;

@Controller
public class EventController {

	@InitBinder("event")
	public void initBinder1(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("event.");
	}

	@Autowired
	private EventDao eventDao;

	@RequestMapping("/event-search")
	public String search(@ModelAttribute Page page) {
		eventDao.page(page);
		return "system/event";
	}

	@RequestMapping("/event-form")
	public String form(@ModelAttribute Event event) {
		return "system/event";
	}

	@RequestMapping("/event-edit")
	public String edit(@RequestParam int id, @ModelAttribute Event event) {
		BeanUtils.copyProperties(eventDao.getById(id), event);
		return "system/event";
	}

	@RequestMapping("/event-saveOrUpdate")
	public String saveOrUpdate(Event event) {
		if (event.getEventId() != null && event.getEventId() != 0) {
			eventDao.update(event);
		} else {
			eventDao.save(event);
		}
		return "forward:/event-search";
	}

}
